<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>递增／递减运算符</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.execution.html">执行运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.logical.html">逻辑运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.operators.increment" class="sect1">
   <h2 class="title">递增／递减运算符</h2>
   <p class="para">
    PHP 支持 C 风格的前／后递增与递减运算符。
   </p>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     递增／递减运算符不影响布尔值。递减
     <strong><code>NULL</code></strong> 值也没有效果，但是递增 <strong><code>NULL</code></strong> 的结果是 <em>1</em>。
    </span>
   </p></blockquote>
   <table class="doctable table">
    <caption><strong>递增／递减运算符</strong></caption>
    
     <thead>
      <tr>
       <th>例子</th>
       <th>名称</th>
       <th>效果</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>++$a</td>
       <td>前加</td>
       <td><var class="varname"><var class="varname">$a</var></var> 的值加一，然后返回 <var class="varname"><var class="varname">$a</var></var>。</td>
      </tr>

      <tr>
       <td>$a++</td>
       <td>后加</td>
       <td>返回 <var class="varname"><var class="varname">$a</var></var>，然后将 <var class="varname"><var class="varname">$a</var></var> 的值加一。</td>
      </tr>

      <tr>
       <td>--$a</td>
       <td>前减</td>
       <td><var class="varname"><var class="varname">$a</var></var> 的值减一， 然后返回 <var class="varname"><var class="varname">$a</var></var>。</td>
      </tr>

      <tr>
       <td>$a--</td>
       <td>后减</td>
       <td>返回 <var class="varname"><var class="varname">$a</var></var>，然后将 <var class="varname"><var class="varname">$a</var></var> 的值减一。</td>
      </tr>

     </tbody>
    
   </table>

   <p class="para">
    一个简单的示例脚本：
    <div class="informalexample">
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"&lt;h3&gt;Postincrement&lt;/h3&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;5:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">++&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;6:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h3&gt;Preincrement&lt;/h3&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;6:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;++</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;6:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h3&gt;Postdecrement&lt;/h3&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;5:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">--&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;4:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h3&gt;Predecrement&lt;/h3&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;4:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;--</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Should&nbsp;be&nbsp;4:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </p>
   <p class="para">
    在处理字符变量的算数运算时，PHP 沿袭了 Perl 的习惯，而非 C 的。例如，在
    Perl 中 <em>$a = &#039;Z&#039;; $a++;</em> 将把 <em>$a</em>
	变成<em>&#039;AA&#039;</em>，而在 C 中，<em>a = &#039;Z&#039;; a++;</em> 将把
	<em>a</em> 变成 <em>&#039;[&#039;</em>（<em>&#039;Z&#039;</em>
	的 ASCII 值是 90，<em>&#039;[&#039;</em> 的 ASCII 值是
	91）。注意字符变量只能递增，不能递减，并且只支持纯字母（a-z 和
	A-Z）。递增／递减其他字符变量则无效，原字符串没有变化。
    <div class="example" id="example-122">
     <p><strong>Example #1 涉及字符变量的算数运算</strong></p>
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'W'</span><span style="color: #007700">;<br />for&nbsp;(</span><span style="color: #0000BB">$n</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">&lt;</span><span style="color: #0000BB">6</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$n</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;++</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
X
Y
Z
AA
AB
AC
</pre></div>
     </div>
    </div>
   </p>
   <p class="para">
    递增或递减布尔值没有效果。
   </p>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.operators.execution.html">执行运算符</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.operators.logical.html">逻辑运算符</a></div>
 <div class="up"><a href="language.operators.html">运算符</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
